package com.hlm.transparent.util;

import org.springframework.util.StringUtils;

/**
 * @program:transparent
 * @description:16进制转换工具包
 * @author: xinY
 **/
public class HexUtil {
    /**
     * 16进制字符串转换成byte
     * @param hexStr
     * @return
     */
    public static byte hexStrToByte(String hexStr) {
        byte highDit = (byte) (Character.digit(hexStr.charAt(0), 16) & 0xFF);
        byte lowDit = (byte) (Character.digit(hexStr.charAt(1), 16) & 0xFF);
        return (byte) (highDit << 4 | lowDit);
    }

    /**
     * 16进制字符串转换成byte[]
     * @param hexString
     * @return
     */
    public static byte[] hexStringToByteArray(String hexString) {
        if (StringUtils.isEmpty(hexString))
            throw new IllegalArgumentException("this hexString must not be empty");

        hexString = hexString.toLowerCase().replaceAll("\\s*","");
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;
        for (int i = 0; i < byteArray.length; i++) {//因为是16进制，最多只会占用4位，转换成字节需要两个16进制的字符，高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }
        return byteArray;
    }

    /**
     * byte[] 转换成16进制字符串
     * @param byteArray
     * @return
     */
    public static String byteArrayToHexString(byte[] byteArray) {
        if (byteArray == null || byteArray.length < 1)
            throw new IllegalArgumentException("this byteArray must not be null or empty");

        final StringBuilder hexString = new StringBuilder();
        for (int i = 0; i < byteArray.length; i++) {
            if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零
                hexString.append("0");
            hexString.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return hexString.toString().toLowerCase();
    }

    /**
     * int 转换成16进制字符串
     * @param i
     * @return
     */
    public static String intToHexString(int i){
        return Integer.toHexString(i);
    }

    /**
     * byte转换咸亨16进制字符串
     * @param b
     * @return
     */
    public static String byteToHexStr(byte b) {
        int v = b & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            hv = "0" + hv;
        }
        return hv;
    }

    /**
     * byte 转换成BCD
     * @param b
     * @return
     */
    public static Integer getBCD(byte b){
        return Integer.valueOf(byteToHexStr(b));
    }

    /**
     * int 转成 2个字节的byte数组
     * @param i
     * @return
     */
    public static byte[] intTo2Byte(Integer i){
        byte[] result = new byte[2];
        //由高位到低位
        result[0] = (byte)((i >> 8) & 0xFF);
        result[1] = (byte)(i & 0xFF);
        return result;
    }

    /**
     * int 转成4个字节的byte数组
     * @param i
     * @return
     */
    public static byte[] intTo4Byte(Integer i){
        byte[] result = new byte[4];
        //由高位到低位
        result[0] = (byte)((i >> 24) & 0xFF);
        result[1] = (byte)((i >> 16) & 0xFF);
        result[2] = (byte)((i >> 8) & 0xFF);
        result[3] = (byte)(i & 0xFF);
        return result;
    }

    /**
     * 一个代表数字的String 转换成byte
     * @param intStr
     * @return
     */
    public static byte intStrToByte(String intStr){
        int i = Integer.parseInt(intStr);
        return (byte) i;
    }
}
